package juf.adapter;



import java.awt.Component;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import juf.Property;
import juf.ui.JufForm;

/**Allows a generic object to be displayed on the form (as its toString() representation)
 * and edited by clicking a button, which displays the object in a popup window
 * 
 * This is the default adapter.
 * 
 * @author Dale Halliwell
 */

@SuppressWarnings("serial")
public class GenericObjectAdapter extends AbstractAdapter implements Adapter {
	JButton buttonDisplayForm;
	JLabel labelObjectName;
	
	/**Create a new GenericObjectAdapter
	 * @param property the Property to bind to
	 */
	public GenericObjectAdapter(Property property) { 
		buttonDisplayForm = new JButton("Edit");
		labelObjectName  = new JLabel(); //this will be set by the first call to refresh()
		
		//the component in this case, is a jpanel containing the name and button
		//the 'real' data component is the labelObjectName
		this.component = new JPanel();
		((JPanel) this.component).setLayout(new BoxLayout(((JPanel) this.component), BoxLayout.X_AXIS));
		((JPanel) this.component).add(labelObjectName);	
		((JPanel) this.component).add(Box.createRigidArea(new Dimension(5,0)));
		((JPanel) this.component).add(buttonDisplayForm);
				
		//listen for the button click
		buttonDisplayForm.addActionListener( //then open object for editing in a JufForm
               new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                	if (GenericObjectAdapter.this.property.getValue() != null){  //TODO if null, then should add a new object of this type
                		System.out.println("Opening: " + GenericObjectAdapter.this.property.getValue().toString());
	   		        	JufForm jufForm = new JufForm(GenericObjectAdapter.this.property.getValue());
						JFrame popup = new JFrame("JufForm"); //create the window frame
						popup.getContentPane().add(jufForm); 
						popup.pack(); //resize the window to suit the JufForm
						popup.setVisible(true); //show the window
			        	//that JufForm should callback a function in JListAdapter that updates the label
                	}
                   }
               }
           );
		
		//bind the property to this adapter
		bind(property, new Dimension(350,30));
	}
	
	/* (non-Javadoc)
	 * @see adapters.Adapter#refresh()
	 */
	@Override
	public void refresh() {
		if (property.getValue() != null)
			this.labelObjectName.setText(property.getValue().toString());
			
	}

	/* (non-Javadoc)
	 * @see adapters.Adapter#save()
	 */
	@Override
	public void save() {
		//just redisplay the control, updating from the object. 
		//any changes to the object will have been
		//saved by the jufform that was created around it
		refresh(); //so just update from the Property
	}
	
	/**
	 * Returns the label displaying the name of the object
	 * as defined by its toString() representation or by annotation.
	 * <p/>
	 * Overrides the default getDataComponent() in AbstractAdapter which
	 * would otherwise return the JPanel that contains this label and the button.
	 * <p/>
	 * @return label bound to the object
	 */
	@Override
	public Component getDataComponent(){
		return (Component) this.labelObjectName;
	}


}
